<?php

namespace app\api\controller;

use app\common\controller\Api;
use think\Config;
use think\Request;
use think\Db;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $this->success('请求成功');
    }
	/**
     * 获取openid
     *
     */
	public function get_openid(){
		$code=input('code');
		if(empty($code)){
			$this->error('code无效');
		}
		$weixin=get_wx_config();
		$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $weixin['appid'] . "&secret=" . $weixin['appsecret'] . "&js_code=" . $code . "&grant_type=authorization_code";
		$arr = $this->vget($url);  
		$arr = json_decode($arr, true);
		if(isset($arr['errcode'])&&$arr['errcode']!=0){
			$data['sta']=0;
			$data['msg']="获取openid失败";
			return json($data);
		}
		$openid = $arr['openid'];
		$session_key = $arr['session_key'];
		if(!$session_key||!$openid){
			$this->error('获取openid失败');
		}
		$user=db('user')->field('nickname,avatar,openid,id')->where('openid',$openid)->find();
		if(empty($user)){
			$data['sta']=0;
		}else{
			$data['sta']=1;
			$data['uid']=$user['id'];
			$data['user']=$user;
		}
		$data['session_key']=$session_key;
		$data['openid']=$openid;
		$data['show_icon']=1;
		return json($data);
	}
	public function get_users(){
		$signature=input('signature');
		$rawData =input("rawData", '', 'htmlspecialchars_decode');
		$session_key =input("session_key");
		$openid=input('openid');
		$signature2 = sha1($rawData . $session_key);
		if ($signature != $signature2) {
		   return json(['code' => 500, 'msg' => '数据签名验证失败！']);
		}
		$user=db('user')->field('nickname,avatar,openid,id')->where('openid',$openid)->find();
		if(!empty($user)){
			$data['sta']=1;
			$data['uid']=$user['id'];
			$data['user']=$user;
			return json($data);
		}
		$rawData=json_decode($rawData,true); 
		$user['nickname']=$rawData['nickName'];
		$user['avatar']=$rawData['avatarUrl'];
		$user['gender']=$rawData['gender'];
		$user['openid']=$openid;
		$user['status']='normal';
		$user['group_id']=1;
		Db::startTrans();
		try {
			$uid=db('user')->insertGetId($user);
			Db::commit();
			$data['sta']=1;
			$data['uid']=$uid;
			$data['user']=$user;
			return json($data);
		}catch (\Exception $e) {
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
        } 
	}
	public function vget($url){
		  $info=curl_init();
		  curl_setopt($info,CURLOPT_RETURNTRANSFER,true);
		  curl_setopt($info,CURLOPT_HEADER,0);
		  curl_setopt($info,CURLOPT_NOBODY,0);
		  curl_setopt($info,CURLOPT_SSL_VERIFYPEER, false);
		  curl_setopt($info,CURLOPT_SSL_VERIFYHOST, false);
		  curl_setopt($info,CURLOPT_URL,$url);
		  $output= curl_exec($info);
		  curl_close($info);
		  return $output;
	}
	//**店铺信息
	public function get_shop(){
		$uid=input('uid');
		$shop=db('ShopAdmin')->field('horse_shop_admin.user_id as uid,horse_shop_admin.shop_id as shopid,shop.*')->join('shop','shop.id=horse_shop_admin.shop_id')->where('horse_shop_admin.user_id',$uid)->group("shop.id")->select();
 
		if(empty($shop)){
			$data['sta']=0;
			$data['msg']='无店铺信息';
		}else{
			$data['sta']=1;
			$data['msg']=$shop;
		}
		return json($data);
	}
	//
	public function shop_view(){
		$uid=input('uid');
		$user=db('user')->find($uid);
		if(empty($user)){
			$data['sta']=0;
			$data['msg']="用户信息为空";
			return json($data);
		}elseif(empty($user['mobile'])){
			$data['sta']=1;
			$data['msg']="去获取手机号";
			return json($data);
		}else{
			$data['sta']=2;
			$data['msg']=$user['mobile'];
			return json($data);
		}
	}
	 
	public function get_mobile(){
		$code=input('code');
		if(empty($code)){
			$data['sta']=0;
			$data['msg']="code无效";
			return json($data);
		}
		$weixin=get_wx_config();
		$encryptedData=input('encryptedData');
		 
		$encryptedData=str_replace(" ","+",$encryptedData);
		 
		$iv=input('iv');
		$uid=input('uid');
		$session_key=$this->get_session_key($code);
		$pc=new \wxBizDataCrypt\wxBizDataCrypt($weixin['appid'],$session_key);
		$errCode = $pc->decryptData($encryptedData,$iv,$daata); //其中$data包含用户的所有数据
		$modata = json_decode($daata,true);
		if ($errCode == 0) {
			$user['id']=$uid;			
			$user['mobile']=$modata['phoneNumber'];
			Db::startTrans();
			try{
				db('user')->update($user);
				
				Db::commit();
				$data['sta']=1;
				$data['msg']=$modata['phoneNumber'];
				return json($data);
			}catch(\Exception $e){
				Db::rollback();
				$data['sta']=0;
				$data['msg']=$e->getMessage();
				return json($data);
			}
		     
		} else {
		    $data['sta']=0;
			$data['msg']="错误码：".$errCode;
			return json($data);
		}
	}
	public function get_session_key($code=""){
		$weixin=get_wx_config();
		$url = "https://api.weixin.qq.com/sns/jscode2session?appid=" . $weixin['appid'] . "&secret=" . $weixin['appsecret'] . "&js_code=" . $code . "&grant_type=authorization_code";
		$arr = $this->vget($url);  
		$arr = json_decode($arr, true);
		if(isset($arr['errcode'])&&$arr['errcode']!=0){
			$data['sta']=0;
			$data['msg']="获取session_key失败";
			return json($data);
		}
		$session_key = $arr['session_key'];
		if(!$session_key){
			$this->error('获取session_key失败');
		}
		 
		return $session_key;
	}
	public function shop_add(){
		$user_id=input('uid');
		if(empty($user_id)){
			$data['sta']=0;
			$data['msg']="参数有误";
			return json($data);
		}
		$user=db('user')->find($user_id);
		if(empty($user)){
			$data['sta']=0;
			$data['msg']="用户信息有误";
			return json($data);
		}
		 
		
		$uname=input('uname');
		if(empty($uname)){
			$data['sta']=0;
			$data['msg']="请输入管理者姓名";
			return json($data);
		}
		$mobile=input('mobile');
		if(empty($mobile)){
			$data['sta']=0;
			$data['msg']="请输入客服电话";
			return json($data);
		}
		$name=input('name');
		if(empty($name)){
			$data['sta']=0;
			$data['msg']="请输入店铺名称";
			return json($data);
		}
		$times=input('times');
		if(empty($times)){
			$data['sta']=0;
			$data['msg']="请输入营业时间";
			return json($data);
		}
		$addr=input('addr');
		if(empty($addr)){
			$data['sta']=0;
			$data['msg']="请输入店铺地址";
			return json($data);
		}
		$images=input('images');
		if(empty($images)){
			$data['sta']=0;
			$data['msg']="请上传图片";
			return json($data);
		}
		$lat=input('lat');
		$lng=input('lng');
		$address=input('address');
		if(empty($lat)||empty($lng)){
			$data['sta']=0;
			$data['msg']="请选择店铺位置";
			return json($data);
		}
		$shop['user_id']=$user_id;
		$shop['mobile']=$mobile;
		$shop['name']=$name;
		$shop['uname']=$uname;
		$shop['times']=$times;
		$shop['addr']=$addr;
		$shop['address']=$address;
		$shop['lat']=$lat;
		$shop['lng']=$lng;
		$shop['images']=$images;
		$shop['createtime']=time();
		Db::startTrans();
		try{
			$shop_id=db('shop')->insertGetId($shop);
			$admin['shop_id']=$shop_id;
			$admin['user_id']=$user_id;
			$admin['is_admin']=1;
			$admin_id=db('ShopAdmin')->insertGetId($admin);
			Db::commit();
			$data['sta']=1;
			$data['msg']="更新成功";
			return json($data);
		}catch(\Exception $e){
			Db::rollback();
			$data['sta']=0;
			$data['msg']=$e->getMessage();
			return json($data);
		}
		 
	}
	
	

	
}
